package net.zjitc.servlet.goods;

import net.zjitc.domain.Goods;
import net.zjitc.service.GoodsService;
import net.zjitc.service.impl.GoodsServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddGoodServlet",urlPatterns = "/addGood")
public class AddGoodServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("UTF-8");
        //1、获取前端传过来的数据
        String name = request.getParameter("name");
        String price = request.getParameter("price");
        String info = request.getParameter("info");

        Goods goods = new Goods(name, Float.parseFloat(price),info);
        //2、把数据添加到数据库
        GoodsService goodsService = new GoodsServiceImpl();
        int num = goodsService.add(goods);
        //3、显示最新列表
        //if (num>0){
         //请示转发
          /*
            RequestDispatcher dispatcher = request.getRequestDispatcher("/goodsList");
            dispatcher.forward(request,response);
            */

        //}
        //自动获取项目路径
        String contextPath = request.getContextPath();
        response.sendRedirect(contextPath+"/goodsList");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
